<%@page import="com.example.myproject.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.*" %>
    <%@ page import="java.io.IOException"%>
    <%@ page import="javax.servlet.http.*"%>
    <%@ page import="java.net.MalformedURLException" %>
    <%@ page import="javax.xml.parsers.DocumentBuilder" %>
    <%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
    <%@ page import="javax.xml.parsers.ParserConfigurationException" %>
    <%@ page import="org.w3c.dom.Document" %>
    <%@ page import="org.w3c.dom.Node" %>
    <%@ page import="org.w3c.dom.NodeList" %>
    <%@ page import="org.xml.sax.SAXException" %>
    <%@ page import="com.google.storage.onestore.v3.OnestoreEntity.Path.Element" %>
    
   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>House Search</title>
<script>
function displayMAP()
{
	var xmlhttp;
	//var xmlhttp1;
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	//  xmlhttp1=new XMLHttpRequest();	
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	//  xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP"); 
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("myMap").innerHTML=xmlhttp.responseText;
	    }
	  }

	alert("I am in displayMAP()");
	xmlhttp.open("GET","map-simple.jsp",true);
	xmlhttp.send();
}

</script>
</head>
<body>

<%
	
	String centreLocation = request.getParameter("centreLocation");
	DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
	DocumentBuilder builder = fact.newDocumentBuilder();
	Document doc = builder.parse("http://maps.googleapis.com/maps/api/geocode/xml?address=" + centreLocation + "&sensor=false");
	NodeList latlist;
	NodeList lnglist;
	Element xmlElement;
	latlist = doc.getElementsByTagName("lat");
	lnglist = doc.getElementsByTagName("lng");


	Node latNode = latlist.item(1);
	Node lngNode= lnglist.item(1);
	String latPoint = ((Double)(Double.parseDouble(latNode.getTextContent()) + 0.003)).toString();
	String lngPoint = ((Double)(Double.parseDouble(lngNode.getTextContent()) + 0.002)).toString();
	out.println("Latitude:"+ latPoint);
	out.println("Longitude:"+lngPoint);
	session.setAttribute("centreLat",latPoint);
	session.setAttribute("centreLng",lngPoint);
	double doubleLatPoint = Double.parseDouble(latPoint);
	double doubleLngPoint = Double.parseDouble(lngPoint);
	HouseRentalLocations searchHelper = new HouseRentalLocations(doubleLatPoint,doubleLngPoint,(double)50.00);
	
	ArrayList<House> nearestHouse = searchHelper.getArrayList();
	Iterator<House> searchedIterator = nearestHouse.iterator();
	int i =0;
	
	out.write("Total number of posts are :" + nearestHouse.isEmpty() + ":");
	out.write("\nThe selected houses are : \n");
	
	while(searchedIterator.hasNext()){
		// set arrayelements in sessionAttributes
		House selectedHouse = searchedIterator.next();
		out.write(selectedHouse.getOwnerName()+ "\n");
		i++;
	}
	
	out.write("Total number houses searches are : " + i);
	
	out.write("flag value:" + HouseRentalLocations.check );

	//out.write("Search Count :" + i + ":" + searchHelper.check);
	
%>
	
<div id="myMap"> </div>
<button type="button" onclick="displayMAP()"> <STRONG> Refresh Map </STRONG> </button> <br/> <br/>

<a href="map-simple.jsp" target="_blank">View Larger Map</a>

</body>
</html>